using CodeStash.Addin.Core.Interfaces;
using Microsoft.VisualStudio.Shell.Interop;
using System.ComponentModel.Composition;

namespace CodeStash.Addin
{
    [Export(typeof(IStatusBarService))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class StatusBarService : IStatusBarService
    {
        private IVsStatusbar statusBar;

        public void SetStatusBar<T>(T statusBar)
        {
            this.statusBar = statusBar as IVsStatusbar;
        }

        public void SetText(string text)
        {
            if (!IsFrozen)
            {
                statusBar.SetText(text);
            }
        }

        public void Clear()
        {
            if (!IsFrozen)
            {
                statusBar.Clear();
            }
        }

        private bool IsFrozen
        {
            get
            {
                int frozen;
                statusBar.IsFrozen(out frozen);
                return frozen != 0;
            }
        }
    }
}
